package cn.myframe.service.bus.impl;

import cn.myframe.dao.bus.BusCompanyDao;
import cn.myframe.entity.bus.BusCompanyEntity;
import cn.myframe.entity.sys.SysUserEntity;
import cn.myframe.service.BaseServiceImpl;
import cn.myframe.service.bus.BusCompanyService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("busCompanyService")
public class BusCompanyServiceImpl extends BaseServiceImpl<BusCompanyDao,BusCompanyEntity> implements BusCompanyService {
	@Autowired 
	private BusCompanyDao busCompanyDao;

    @Override
    public List<BusCompanyEntity> findOffSpring(Long companyNo) {
        Map<String, Object> map = new HashMap<>();
        map.put("status", "2");
        List<BusCompanyEntity> companyList = baseMapper.queryList(map);
        if (companyNo == null) {
            return null;
        }
        for (int i = 0; i < companyList.size(); i++) {
            if (companyList.get(i).getParentId() == null) {
                companyList.remove(i);
            }
        }
        return getChildren(companyList, companyNo);
    }

    private List<BusCompanyEntity> getChildren(List<BusCompanyEntity> list, Long parentId) {
        List<BusCompanyEntity> children = new ArrayList<>();
        for (BusCompanyEntity item : list) {
            if (Objects.equals(parentId, item.getParentId())) {
                children.add(item);
                List<BusCompanyEntity> itemChildren = getChildren(list, item.getCompanyNo());
                if (itemChildren.size() > 0) {
                    children.addAll(itemChildren);
                }
            }
        }
        return children;
    }

	@Override
	public List<SysUserEntity> queryUser(Long companyId) {
		return busCompanyDao.queryUser(companyId);
	}

	@Override
	public List<Long> queryParentCompany(Long companyNo){
		return busCompanyDao.queryParentCompany(companyNo);
	}

}

